package com.weme.common.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.weme.common.R

class SmoothRoundedRectView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
    private var paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var path: Path = Path()
    private var rectF: RectF = RectF()
    var roundPath: SmootRoundedPath = SmootRoundedPath()
    private var leftRadius = 0F
    private var rightRadius = 0F
    private var topRadius = 0F
    private var bottomRadius = 0F
    private var rectColor = Color.WHITE
    private var drawBorderOnly = false
    private var elevation = 0f
    private val shadowColor = Color.parseColor("#1A000000")
    private var startColor = Color.WHITE
    private var endColor = Color.WHITE

    init {
        val a = context.obtainStyledAttributes(attrs, R.styleable.SmoothRoundedRectView, defStyleAttr, 0)
        val cornerRadius = a.getDimensionPixelSize(R.styleable.SmoothRoundedRectView_radius, -1)
        leftRadius = a.getDimensionPixelSize(R.styleable.SmoothRoundedRectView_rightTopRadius, cornerRadius).toFloat()
        rightRadius = a.getDimensionPixelSize(R.styleable.SmoothRoundedRectView_leftBottomRadius, cornerRadius).toFloat()
        topRadius = a.getDimensionPixelSize(R.styleable.SmoothRoundedRectView_rightBottomRadius, cornerRadius).toFloat()
        bottomRadius = a.getDimensionPixelSize(R.styleable.SmoothRoundedRectView_leftTopRadius, cornerRadius).toFloat()
        rectColor = a.getColor(R.styleable.SmoothRoundedRectView_rectColor, Color.WHITE)
        drawBorderOnly = a.getBoolean(R.styleable.SmoothRoundedRectView_drawBorderOnly, false)
        elevation = a.getDimensionPixelSize(R.styleable.SmoothRoundedRectView_elevation, 0).toFloat()
        startColor = a.getColor(R.styleable.SmoothRoundedRectView_smoothStartColor, Color.WHITE)
        endColor = a.getColor(R.styleable.SmoothRoundedRectView_smoothEndColor, 0)

        val gradientOrientation = a.getInt(R.styleable.SmoothRoundedRectView_gradientOrientations, -1)

        if (startColor != endColor&&endColor!=0) {
            // 如果设置了渐变颜色
            val orientation = if (gradientOrientation == 0) {
                // 如果设置为横向渐变
                Shader.TileMode.CLAMP
            } else {
                // 如果设置为竖向渐变
                Shader.TileMode.MIRROR
            }

            // 创建线性渐变
            val gradient = LinearGradient(
                0f, 0f, width.toFloat(), height.toFloat(),
                startColor, endColor, orientation
            )

            // 设置渐变色
            paint.shader = gradient
        }

        a.recycle()
        init()
    }

    private fun init() {
        paint.color = rectColor
        paint.style = Paint.Style.FILL
        paint.setShadowLayer(elevation, 0F, 10F, shadowColor)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if(leftRadius!=0F) leftRadius = (height/5).toFloat()
        if(topRadius!=0F)   topRadius = (height/5).toFloat()
        if(rightRadius!=0F)   rightRadius = (height/5).toFloat()
        if(bottomRadius!=0F)  bottomRadius = (height/5).toFloat()
        // 计算圆角矩形的边界
        rectF.set(0f, 0f, width.toFloat(), height.toFloat())
        // 绘制圆角矩形路径
        path = roundPath.getSmoothRoundPath(
            rectF,
            Math.max(leftRadius, 0f),
            Math.max(topRadius, 0f),
            Math.max(rightRadius, 0f),
            Math.max(bottomRadius, 0f)
        )

        if (paint.shader == null) {
            // 如果着色器为 null，则绘制单一颜色的背景
            if (drawBorderOnly) {
                // 仅绘制边框
                paint.style = Paint.Style.STROKE
                paint.strokeWidth = 1F // 设置边框宽度，可以根据需要自行定义
            } else {
                // 绘制圆角矩形
                paint.style = Paint.Style.FILL
            }
        } else {
            // 如果着色器不为 null，则绘制渐变背景
            paint.style = Paint.Style.FILL
        }

        // 绘制圆角矩形或边框
        canvas.drawPath(path, paint)
    }
}
